package com.example.soa2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa2.common.CommonResponse;
import com.example.soa2.entity.Material;
import com.example.soa2.persistence.MaterialMapper;
import com.example.soa2.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("MaterialService")
public class MaterialServiceImpl implements MaterialService
{
    @Autowired
    private MaterialMapper materialMapper;

    @Override
    public CommonResponse<Material> getMaterial(int id)
    {
        return CommonResponse.createForSuccess(materialMapper.selectById(id));
    }

    @Override
    public CommonResponse<Material> getMaterial(String name)
    {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return CommonResponse.createForSuccess(materialMapper.selectOne(queryWrapper));
    }

    @Override
    public CommonResponse<List<Material>> getMaterials()
    {
        return CommonResponse.createForSuccess(materialMapper.selectList(null));
    }

    @Override
    public CommonResponse<Material> addMaterial(Material material) {
        QueryWrapper<Material> materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.select("MAX(id)");
        materialMapper.selectObjs(materialQueryWrapper);
        List<Object> maxIds = materialMapper.selectObjs(materialQueryWrapper);
        if (!maxIds.isEmpty()) {
            material.setId((int) maxIds.get(0) + 1);
        } else {
            material.setId(1);
        }
        int result = materialMapper.insert(material);
        if (result == 0) {
            return CommonResponse.createForError("新增失败");
        }
        return CommonResponse.createForSuccessMessage("新增成功");
    }

    @Override
    public CommonResponse<Material> deleteMaterial(int id) {
        int result = materialMapper.deleteById(id);
        if (result == 0) {
            return CommonResponse.createForError("删除资源失败");
        }
        return CommonResponse.createForSuccess();
    }

    @Override
    public CommonResponse<String> updateMaterial(int id, Material material) {
        try {
            Material existingRecord = materialMapper.selectById(id);

            if (existingRecord == null) {
                return CommonResponse.createForError("更新失败，记录不存在");
            }

            // Perform the update operation based on the fields you want to update
            existingRecord.setId(material.getId());
            existingRecord.setName(material.getName());
            existingRecord.setCategory(material.getCategory());
            existingRecord.setQuantity(material.getQuantity());

            int updateResult = materialMapper.updateById(existingRecord);

            if (updateResult > 0) {
                return CommonResponse.createForSuccess("更新成功");
            } else {
                return CommonResponse.createForError("更新失败");
            }
        } catch (Exception e) {
            return CommonResponse.createForError("更新失败：" + e.getMessage());
        }
    }
}
